<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/26 14:33
 */

namespace app\admin\model;


class Banner extends DataBase
{
    protected $name = 'banner';
    protected $autoWriteTimestamp = true;
//    protected $updateTime = 'update_time';

    public function getStartTimeAttr($value=0)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getEndTimeAttr($value=0)
    {
        return date('Y-m-d H:i:s', $value);
    }
    /**
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {

        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        $start_time =strtotime(input('start_time'));
        $end_time = strtotime(input('end_time'));
        $key = input('key');
        $value = input('value');

        if(empty($start_time)) $start_time = 0;
        if(empty($end_time)) $end_time = time();

        $where[] = ['created_at','>=',$start_time];
        $where[] = ['created_at','<',$end_time];

        if(!empty($key) && !empty($value)){

            $where[] = [$key,'=',$value];
            // halt($map);
        }

        return $this
            ->where($where)
            ->order('id desc')
            ->paginate($page_config);
    }

    public function add()
    {
        $map = ['u_id'=>'u_number','id'=>'user_id','r_id'=>'user_id'];
        //halt($_FILES);
        $newData['banner_name'] = input('post.banner_name');
        $newData['status'] = input('post.status');
        $newData['sort'] = input('post.sort');
        $newData['start_time'] = strtotime(input('post.start_time'));
        $newData['end_time'] = strtotime(input('post.end_time'));
        $newData['img_url'] =  oosUpload($_FILES['img_url'],'ss-banner');
        $newData['bannerandroid_url'] = input('post.bannerandroid_url');
        $newData['bannerios_url'] = input('post.bannerios_url');
        if(!empty($_FILES['android_url']['name'])) $newData['bannerandroid_url'] = oosUpload($_FILES['android_url'],'qqbanner');
        if(!empty($_FILES['ios_url']['name'])) $newData['bannerios_url'] = oosUpload($_FILES['ios_url'],'ss-banner');
        $type = input('post.type');
        if(!empty($type)){
            $userId = User::where('u_number','=',$type)->value('user_id');
            if(empty($userId)) return ['code'=>-100,'msg'=>'用户不存在'];
            $newData['roomId'] = Room::where('user_id','=',$userId)->value('id');
            if(empty($newData['roomId']))  return ['code'=>-100,'msg'=>'房间不存在'];
            $newData['type'] = 2;
        }else{
            $newData['type'] = 0;
            $newData['roomId'] = null;
        }
        $res = $this->save($newData);
        return $res;
    }

    public function edit()
    {
        $id = input('get.id');
        $data = $this->where('id','=',$id)->find();
        return $data;
    }

    public function editData()
    {
        $id = input('get.id');
        $newData = input('get.');

        $newData['start_time'] = strtotime($newData['start_time']);
        $newData['end_time'] = strtotime($newData['end_time']);

        $res = $this->save($newData,['id'=>$id]);

        if($res)  return ['code'=>100,'msg'=>'修改成功'];
        return ['code'=>-100,'msg'=>'修改失败'];
    }
}